package com.mall.b2c.domain.store;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mall.b2c.bean.StoreBean;
import com.mall.b2c.util.SettingUtils;
import com.mall.common.domain.AreaDomain;
import com.mall.common.domain.BaseEntity;
import com.mall.common.domain.PriorityEntity;
import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.util.Date;
import java.util.Set;

/**
 * Created by Misun on 2016/12/21.
 * 门店
 */
@Entity
@Table(name = "b2c_store")
public class StoreDomain extends PriorityEntity {

    private String name;//门店名称
    private String index_img;//门店首页图片
    private String address;//地址
    private String route;//交通路线
    private String telephone;//联系电话
    private String contact;//联系人
    private String opeanTime;//营业时间
    private String api_address;//地图定位
    private String xpoint;//x坐标
    private String ypoint;//y坐标
    private String brief;//门店简介
    private String biz_license;//营业执照
    private String features;//经营特色
    private String seo_title;//seo标题
    private String seo_keyword;//seo关键字
    private String seo_description;//seo描述
    private Boolean is_effect;//是否有效
    private Boolean is_recommend;//是否是推荐门店
    private Boolean isDefault; //默认门店

    private String code;   //层级树编码
    private @JsonIgnore StoreDomain upDomain;
    @JsonIgnore private Set<StoreDomain> children;

    @JsonIgnore
    private StoreCityDomain city;


    @PrePersist
    public void prePersist(){
        if(isDefault == null) setIsDefault(false);
    }

    @PreUpdate
    public void preUpdate(){
        if(isDefault == null) setIsDefault(false);
    }
    /**
     * 默认根为企业
     * id=企业的id
     * @return
     */
    @Transient
    public static StoreDomain genRoot(){
        StoreDomain store = new StoreDomain();
        store.setName(SettingUtils.get().getName());
        return store;
    }

    @Transient
    public Long getParentId() {
        StoreDomain parent = getUpDomain();
        if (parent != null) {
            return parent.getId();
        } else {
            return null;
        }
    }

    @Transient
    @JsonIgnore
    public StoreBean getBean(){
        StoreBean bean = new StoreBean();
        bean.setName(this.getName());
        bean.setCityName(this.getCity().getName());
        bean.setCityId(this.getCity().getId());
        bean.setId(this.getId());
        return bean;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIndex_img() {
        return index_img;
    }

    public void setIndex_img(String index_img) {
        this.index_img = index_img;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getRoute() {
        return route;
    }

    public void setRoute(String route) {
        this.route = route;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getOpeanTime() {
        return opeanTime;
    }

    public void setOpeanTime(String opeanTime) {
        this.opeanTime = opeanTime;
    }

    public String getApi_address() {
        return api_address;
    }

    public void setApi_address(String api_address) {
        this.api_address = api_address;
    }

    public String getXpoint() {
        return xpoint;
    }

    public void setXpoint(String xpoint) {
        this.xpoint = xpoint;
    }

    public String getYpoint() {
        return ypoint;
    }

    public void setYpoint(String ypoint) {
        this.ypoint = ypoint;
    }

    public String getBrief() {
        return brief;
    }

    public void setBrief(String brief) {
        this.brief = brief;
    }

    public Boolean getIs_effect() {
        return is_effect;
    }

    public void setIs_effect(Boolean is_effect) {
        this.is_effect = is_effect;
    }

    public Boolean getIs_recommend() {
        return is_recommend;
    }

    public void setIs_recommend(Boolean is_recommend) {
        this.is_recommend = is_recommend;
    }

    public String getBiz_license() {
        return biz_license;
    }

    public void setBiz_license(String biz_license) {
        this.biz_license = biz_license;
    }

    public String getFeatures() {
        return features;
    }

    public void setFeatures(String features) {
        this.features = features;
    }

    public String getSeo_title() {
        return seo_title;
    }

    public void setSeo_title(String seo_title) {
        this.seo_title = seo_title;
    }

    public String getSeo_keyword() {
        return seo_keyword;
    }

    public void setSeo_keyword(String seo_keyword) {
        this.seo_keyword = seo_keyword;
    }

    public String getSeo_description() {
        return seo_description;
    }

    public void setSeo_description(String seo_description) {
        this.seo_description = seo_description;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(columnDefinition="bit(1) not null default 0")
    public Boolean getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(Boolean isDefault) {
        this.isDefault = isDefault;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "up_id")
    public StoreDomain getUpDomain() {
        return upDomain;
    }

    public void setUpDomain(StoreDomain upDomain) {
        this.upDomain = upDomain;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "upDomain")
    @Cascade(value = { org.hibernate.annotations.CascadeType.DELETE })
    @OrderBy("createDate desc")
    public Set<StoreDomain> getChildren() {
        return children;
    }

    public void setChildren(Set<StoreDomain> children) {
        this.children = children;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="city_id")
    public StoreCityDomain getCity() {
        return city;
    }

    public void setCity(StoreCityDomain city) {
        this.city = city;
    }
}
